package com.caifu.controller;

import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.service.ZcOrderingRecordsService;
import com.caifu.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Auther: Lyf
 * @Date: 2020/12/8 10:37
 * @Description:
 */
@Controller
@RequestMapping("/foodConfirm")
public class ConfirmController {

    @Autowired
    private ZcOrderingRecordsService orderingRecordsService;

    @GetMapping("/index")
    public String index(ModelMap map, String orgId) {
        //orgId = "1210";
        map.put("orgId",orgId);
        map.put("currentDate", Util.dateToString(null,"yyyy-MM-dd"));
        return "foodDelivery/orderingRecords/confirm";
    }

    @PostMapping("/getFoodType")
    @ResponseBody
    public Result getFoodType(String orgId, String month){
        try{
            String[] arr = month.split("-");
            if(arr != null && arr.length > 0) {
                return new Result(200, "获取当日餐别成功！", orderingRecordsService.getFoodType(orgId, arr[0]+arr[1], arr[2]));
            }else{
                return new Result(-1, "日期格式不正确，获取当日餐别失败！", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"获取当日餐别失败！");
        }
    }

    @PostMapping("/confirm")
    @ResponseBody
    public Result confirm(String orgId, String month,String type){
        try{
            orderingRecordsService.confirm(orgId,month,type);
            return new Result(200, "确认送餐成功！", null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"确认送餐失败！");
        }
    }
}
